//
//  HWVipUserInfoView.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/10.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWVipUserInfoView: UIView {

    var topImageView:UIImageView!
    var bgView:UIView!
    var crownImageView:UIImageView!
    var titleLabel:UILabel!
    /// 上涨
    var styleImageView:UIImageView!
    var styleExpLabel:UILabel!
    var headBgView:UIView!
    var headImageView:UIImageView!
    var headFrameImageView:UIImageView!
    /// 升级剩余
    var upgradeSurplusLabel:UILabel!
    
    var progressBarBg:UIView!
    var progressBar:YLProgressBar!
    /// 时间
    var timeLabel:UILabel!
    /// 开通按钮
    var openButton:UIButton!
    
    var openButtonClick:(()->())?
    
    var model:HWVIPHomeTopModel?{
        didSet{
            headImageView.sd_setImage(with: URL(string: model?.head ?? ""), completed: nil)
            headFrameImageView.sd_setImage(with: URL(string: model?.head_frame ?? ""), completed: nil)
            titleLabel.text = model?.name
            upgradeSurplusLabel.text = "VIP值：\(model?.exp ?? 0)，距离下一级剩余:\((model?.last_exp ?? 0) - (model?.exp ?? 0))"
            timeLabel.text = model?.end_time
            progressBar.progress = CGFloat(model?.exp ?? 0) / CGFloat(model?.last_exp ?? 0)
            crownImageView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            styleImageView.sd_setImage(with: URL(string: model?.style_img ?? ""), completed: nil)
            
            styleExpLabel.text = model?.style_exp
            
            if model?.style_exp.contains("+") == true {
                styleExpLabel.textColor = .colorWithHexString(hexString: "#3BB958")
            }else{
                styleExpLabel.textColor = .colorWithHexString(hexString: "#E51111")
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        UIColor.gradientColor(view: bgView, colors: [UIColor.colorWithHexString(hexString: "#F7F5F3").cgColor,
                                                        UIColor.colorWithHexString(hexString: "#DFCA9B").cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 1, y: 1))
        
//        UIColor.gradientColor(view: headBgView, colors: [UIColor.colorWithHexString(hexString: "#F7F5F3").cgColor,
//                                                         UIColor.colorWithAlphaHexString(hexString: "#DFCA9B", alpha: 0).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 0, y: 0.5))

        UIColor.gradientColor(view: progressBarBg, colors: [UIColor.colorWithHexString(hexString: "#FAF3E0").cgColor,
                                                         UIColor.colorWithAlphaHexString(hexString: "#FAF3E0", alpha: 1).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 1, y: 0))
    
    }

}

extension HWVipUserInfoView {
    private func initUI() -> Void {
        
        
        topImageView = UIImageView()
        topImageView.image = UIImage(named: "vipUserInfoTop")
        addSubview(topImageView)
        
        bgView = UIView()
        bgView.layer.cornerRadius = 29*widthScale
        bgView.layer.masksToBounds = true
        addSubview(bgView)
        
        crownImageView = UIImageView()
        crownImageView.image = UIImage(named: "vipCrown")
        bgView.addSubview(crownImageView)
        
        titleLabel = HWLabel.createLabel(title: "会员用户", color: "#AC8341", font: 20*widthScale)
        titleLabel.font = UIFont.init(name: "YouSheBiaoTiHei", size: 20*widthScale)!
        bgView.addSubview(titleLabel)
        
        styleImageView = UIImageView()
        bgView.addSubview(styleImageView)
        
        styleExpLabel = HWLabel.createLabel(title: "", color: "#3BB958", font: 10*widthScale)
        bgView.addSubview(styleExpLabel)
        
        headBgView = UIView()
        headBgView.layer.cornerRadius = 79/2*widthScale
        headBgView.layer.masksToBounds = true
        headBgView.backgroundColor = .clear
        addSubview(headBgView)
        
        headImageView = UIImageView()
        headImageView.backgroundColor = .clear
        headImageView.layer.cornerRadius = 73/2*widthScale
        headImageView.layer.masksToBounds = true
        headBgView.addSubview(headImageView)
        
        headFrameImageView = UIImageView()
        headFrameImageView.backgroundColor = .clear
        headFrameImageView.layer.masksToBounds = true
        headFrameImageView.contentMode = .scaleAspectFit
        addSubview(headFrameImageView)
        
        upgradeSurplusLabel = HWLabel.createLabel(title: "当前VIP值：88，距离下一级剩余:1000", color: "#C29B5B", font: 10*widthScale)
        bgView.addSubview(upgradeSurplusLabel)
        
        progressBarBg = UIView()
        progressBarBg.layer.cornerRadius = 2*widthScale
        progressBarBg.layer.masksToBounds = true
        addSubview(progressBarBg)
        
        progressBar = YLProgressBar()
        progressBar.indicatorTextDisplayMode = .none
        progressBar.progressTintColors = [UIColor.colorWithHexString(hexString: "#DFCA9B"),UIColor.colorWithHexString(hexString: "#AC8341")]
        progressBar.trackTintColor = .clear
        progressBar.hideStripes = true
        progressBar.progress = 0
        progressBar.hideGloss = true
        progressBar.progressStretch = true
        progressBar.progressBarInset = 0
        addSubview(progressBar)
        
        timeLabel = HWLabel.createLabel(title: "2022-11-10 到期", color: "#AC8341", font: 10*widthScale)
        bgView.addSubview(timeLabel)
        
        openButton = UIButton(type: .custom)
        openButton.setTitle("续费", for: .normal)
        openButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 10)
        openButton.backgroundColor = .colorWithHexString(hexString: "#AC8341")
        openButton.layer.cornerRadius = 8*widthScale
        openButton.addTarget(self, action: #selector(openButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(openButton)
    }
    
    private func initFrame() -> Void {
        topImageView.snp.makeConstraints { make in
            make.top.left.equalTo(0)
        }
        
        bgView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(20*widthScale)
        }
        
        crownImageView.snp.makeConstraints { make in
            make.left.top.equalTo(20*widthScale)
            make.width.height.equalTo(24*widthScale)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(crownImageView.snp.right).offset(6*widthScale)
            make.centerY.equalTo(crownImageView)
        }
        
        styleImageView.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right).offset(15*widthScale)
            make.width.equalTo(5*widthScale)
            make.height.equalTo(10*widthScale)
            make.centerY.equalTo(titleLabel)
        }
        
        styleExpLabel.snp.makeConstraints { make in
            make.left.equalTo(styleImageView.snp.right).offset(2*widthScale)
            make.centerY.equalTo(styleImageView)
        }
        
        headBgView.snp.makeConstraints { make in
            make.width.height.equalTo(79*widthScale)
            make.top.equalTo(bgView.snp.top).offset(-20*widthScale)
            make.right.equalTo(bgView.snp.right).offset(-24*widthScale)
        }
        
        headImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(73*widthScale)
        }
        
        headFrameImageView.snp.makeConstraints { make in
            make.center.equalTo(headImageView)
            make.width.height.equalTo(108*widthScale)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(22*widthScale)
            make.bottom.equalTo(-10*widthScale)
        }
        
        openButton.snp.makeConstraints { make in
            make.right.equalTo(-21*widthScale)
            make.bottom.equalTo(-10*widthScale)
            make.width.equalTo(50*widthScale)
            make.height.equalTo(16*widthScale)
        }
        
        progressBarBg.snp.makeConstraints { make in
            make.left.equalTo(22*widthScale)
            make.height.equalTo(4*widthScale)
            make.right.equalTo(-22*widthScale)
            make.bottom.equalTo(openButton.snp.top).offset(-10*widthScale)
        }
        
        progressBar.snp.makeConstraints { make in
            make.left.equalTo(22*widthScale)
            make.height.equalTo(4*widthScale)
            make.right.equalTo(-22*widthScale)
            make.bottom.equalTo(openButton.snp.top).offset(-10*widthScale)
        }
        
        upgradeSurplusLabel.snp.makeConstraints { make in
            make.left.equalTo(22*widthScale)
            make.bottom.equalTo(progressBar.snp.top).offset(-6*widthScale)
        }
    }
}

extension HWVipUserInfoView {
    @objc func openButtonClick(_ :UIButton) -> Void {
        self.openButtonClick?()
    }
}
